/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bddfirstproject.view;

import bddfirstproject.MainController;
import bddfirstproject.controller.PartieRedactionDAL;
import bddfirstproject.model.PartieRedaction;
import bddfirstproject.model.Theme;
import bddfirstproject.view.renderer.ListArticleRenderer;
import java.util.ArrayList;
import javax.swing.ListModel;

/**
 *
 * @author epulapp
 */
public class ArticlesView extends NamedJPanel {
    
    protected static final String title = "Selection d'un article";
    protected ArrayList<PartieRedaction> datasListArticles;
    /**
     * Creates new form ArticlesView
     */
    public ArticlesView() {
        initComponents();
        
        MainController mainController           = MainController.getInstance();
        datasListArticles = PartieRedactionDAL.getAllArticle(mainController.getCurrentTheme());
        listArticles.setCellRenderer(new ListArticleRenderer());
        listArticles.setListData(datasListArticles.toArray());
        listArticles.setSelectedIndex(0);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        comebackButton = new javax.swing.JButton();
        validButton = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        listArticles = new javax.swing.JList();
        listArticleLabel = new javax.swing.JLabel();

        comebackButton.setText("Retour à la liste des thèmes");
        comebackButton.setMinimumSize(new java.awt.Dimension(165, 25));
        comebackButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                comebackButtonActionPerformed(evt);
            }
        });

        validButton.setText("Selectionner cet article");
        validButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                validButtonActionPerformed(evt);
            }
        });

        listArticles.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        listArticles.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                listArticlesMouseClicked(evt);
            }
        });
        listArticles.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                listArticlesValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(listArticles);

        listArticleLabel.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        listArticleLabel.setText("Selectionner un article : ");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(4, 4, 4)
                        .addComponent(comebackButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(validButton))
                    .addComponent(listArticleLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(listArticleLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 224, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(comebackButton, javax.swing.GroupLayout.DEFAULT_SIZE, 25, Short.MAX_VALUE)
                    .addComponent(validButton))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void comebackButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comebackButtonActionPerformed
        MainController mainController = MainController.getInstance();
        mainController.setCurrentTheme(null);
        mainController.setCurrentView(new ThemeListView());
    }//GEN-LAST:event_comebackButtonActionPerformed

    private void validButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_validButtonActionPerformed
        MainController mainController = MainController.getInstance();
        PartieRedaction currentPartie = (PartieRedaction) listArticles.getSelectedValue();
        System.out.println("Selection de l'article " + currentPartie);
        mainController.setCurrentArticle(currentPartie);
        mainController.setCurrentView(new TreeDocumentsView());
    }//GEN-LAST:event_validButtonActionPerformed

    private void listArticlesValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_listArticlesValueChanged
        if(listArticles.getSelectedValue() == null) validButton.setEnabled(false);
        else validButton.setEnabled(true);
    }//GEN-LAST:event_listArticlesValueChanged

    private void listArticlesMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_listArticlesMouseClicked
     if(evt.getClickCount() == 2)
     {
        int index = listArticles.locationToIndex(evt.getPoint());
        ListModel dataListModel = listArticles.getModel();
        Object item = dataListModel.getElementAt(index);;
        listArticles.ensureIndexIsVisible(index);
        System.out.println("Double clicked on " + item);
        
        //Action à effectuer au double click
        MainController mainController = MainController.getInstance();
        PartieRedaction currentPartie = (PartieRedaction) listArticles.getSelectedValue();
        System.out.println("Selection de l'article " + currentPartie);
        mainController.setCurrentArticle(currentPartie);
        mainController.setCurrentView(new TreeDocumentsView());
     }
    }//GEN-LAST:event_listArticlesMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton comebackButton;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel listArticleLabel;
    private javax.swing.JList listArticles;
    private javax.swing.JButton validButton;
    // End of variables declaration//GEN-END:variables

    @Override
    public String getTitle() {
        return title;
    }
}
